package com.xq.jltx.fragment;

import android.content.Context;
import android.database.Cursor;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;

import com.xq.jltx.adapter.ModefyNoteAdapter;
import com.xq.jltx.db.NoteDbUtils;
import com.xq.jltx.db.bean.Note;
import com.xq.jltx.greendaopro.R;
import com.xq.jltx.layout.DividerItemDecoration;
import com.xq.jltx.listener.OnItemListener;
import com.xq.jltx.utils.XqToast;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author jltxseo
 *         Created by junlintianxia on 2016/07/20.
 */
public class ModefyFragment extends Fragment implements LoaderManager.LoaderCallbacks<Cursor>{
    private EditText noteEditText;
    private Button modefyBtn;
    private RecyclerView noteRecylerView;
    private ModefyNoteAdapter modefyNoteAdapter;

    public ModefyFragment() {
    }

    public static ModefyFragment newInstance(){
        ModefyFragment modefyFragment = new ModefyFragment();
        return modefyFragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.modefy_fragment_layout,container,false);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        assignView(view);
        modefyBtn.setOnClickListener(mOnClickListener);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
        DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(getContext());
//        dividerItemDecoration.setDivider(R.drawable.item_divide_bg);
        noteRecylerView.addItemDecoration(dividerItemDecoration);
        // 设置item动画
        noteRecylerView.setItemAnimator(new DefaultItemAnimator());
        noteRecylerView.setItemAnimator(null);
        noteRecylerView.setLayoutManager(linearLayoutManager);
        modefyNoteAdapter = new ModefyNoteAdapter(getContext(),noteRecylerView,null);
        noteRecylerView.setAdapter(modefyNoteAdapter);
        getLoaderManager().initLoader(0, null, this);
        initEvent();
    }

    private void assignView(View view){
        noteEditText = (EditText)view.findViewById(R.id.note_edittext);
        modefyBtn = (Button)view.findViewById(R.id.modefy_btn);
        noteRecylerView = (RecyclerView)view.findViewById(R.id.my_note_recylerview);
    }

    private void initEvent(){
        modefyNoteAdapter.setmOnItemListener(new OnItemListener() {
            @Override
            public void onItemClick(View view, int position, long id) {

            }

            @Override
            public boolean onItemLongClick(View view, int position, long id) {
                modefyNoteAdapter.setSelectIndex(-1);
                XqToast.showToastShortMsg(getContext(),"删除id="+id+"的数据");
                NoteDbUtils.deleteNote(id);
                //异步加载cursor查询
                getLoaderManager().restartLoader(0,null, ModefyFragment.this);
                return false;
            }
        });
    }

    @Override
    public Loader<Cursor> onCreateLoader(int id, Bundle args) {
        return new MyCursorLoader(getContext());
    }

    @Override
    public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
        modefyNoteAdapter.changeCursor(data);
    }

    @Override
    public void onLoaderReset(Loader<Cursor> loader) {
        modefyNoteAdapter.changeCursor(null);
    }

    private View.OnClickListener mOnClickListener = new View.OnClickListener(){
        @Override
        public void onClick(View v) {
            if(v.getId() == R.id.modefy_btn){
                String text = noteEditText.getText().toString().trim();
                if(!TextUtils.isEmpty(text) && modefyNoteAdapter.getSelectIndex() != -1){
                    SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    Date curDate=new Date();
                    String t1="创建于"+ format.format(curDate);
                    long id = modefyNoteAdapter.getItemId(modefyNoteAdapter.getSelectIndex());
                    Note note = new Note(id,text,t1,curDate);
                    NoteDbUtils.modefyNote(note);
                    //异步加载cursor查询
                    getLoaderManager().restartLoader(0,null, ModefyFragment.this);
                }else {
                    XqToast.showToastShortMsg(getContext(),"输入的文字为空");
                }
            }
        }
    };


    public static class MyCursorLoader extends CursorLoader{
        public MyCursorLoader(Context context) {
            super(context);
        }

        @Override
        public Cursor loadInBackground() {
            return NoteDbUtils.getAllNotesCursor();
        }
    }
}
